﻿using LServer.Core.Logger;
using LServer.Model.Logic.DBModels;
using LServer.Model.Logic.Entitys;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LServer.Model.Test
{
    public class TestMongoDB
    {
        public static void TestAdd() 
        {
            RoleDBModel model = new RoleDBModel();
            UniqueIDGameServer uniqueIDGameServer = new UniqueIDGameServer();

            for (int i = 0; i < 10000; i++)
            {
                RoleEntity entity = new RoleEntity();

                entity.LFId = uniqueIDGameServer.GetUniqueID((int)CollectionType.Role);
                entity.NickName = "xxl" + i;
                entity.CreateTime = DateTime.Now;
                entity.UpdateTime = DateTime.Now;

                entity.TaskList.Add(new TaskEntity { TaskId = 1, CurrStatus = 0 });
                entity.TaskList.Add(new TaskEntity { TaskId = 2, CurrStatus = 1 });

                entity.SkillDic[1] = new SkillEntity() { SkillId = 1, CurrLevel = 2 };
                entity.SkillDic[2] = new SkillEntity() { SkillId = 2, CurrLevel = 4 };
                entity.SkillDic[3] = new SkillEntity() { SkillId = 3, CurrLevel = 6 };

                model.Add(entity);

                LoggerMgr.Log(Core.LoggerLevel.Log, 0, "Add Role LFID={0}", entity.LFId);
            }
        }


        public static void TestSearch() 
        {
            RoleDBModel model = new RoleDBModel();
            FilterDefinition<RoleEntity> filter = Builders<RoleEntity>.Filter.Eq(t => t.LFId, 20);

            //1. id 查不到的实体 是不会为null 返回的是一个 新创建的实体
            RoleEntity roleEntity =  model.GetEntity(1);

            //2. roleEntity.SkillDic 没有的字段也不会为 null，会返回默认值 类的话，会new 一个
            Console.WriteLine("xxl 角色实体是否为null ：： " + roleEntity.SkillDic == null);
            roleEntity.SkillDic[1] = new SkillEntity() { SkillId = 1, CurrLevel = 2 };

            model.Update(roleEntity);
        }
    }
}
